<template>
  <view class="change-password-page">

     <header class="header">
      <image class="header-image" src="/static/header-bg.png" mode="aspectFill"></image>
      
      <!-- 返回按钮，与登录页一致 -->
      <view class="back-button" @click="goBack">
        <image class="back-icon" src="/static/back-icon.png" mode="aspectFit"></image>
      </view>
      
      <view class="hero-content">
        <!-- 左侧文本区域 -->
        <view class="hero-text">
          <text class="welcome">更改密码</text>
          <!-- <text class="slogan">好玩世界，尽在社交搭子</text> -->
        </view>
        
        <!-- 右侧图片区域 -->
        <view class="hero-image">
          <image src="/static/welcome-image.png" mode="aspectFit"></image>
        </view>
      </view>
    </header>

    <!-- 表单卡片 -->
    <view class="card">
      <view class="field">
        <text class="label">你的手机号</text>
        <view class="phone-display">{{ phone || '未绑定' }}</view>
      </view>
      <view class="divider"></view>
      
      <view class="field row">
        <view class="col">
          <text class="label">验证码</text>
          <input class="input" v-model="code" type="number" placeholder="请输入验证码" maxlength="6" />
        </view>
        <button class="code-btn" :disabled="countdown>0" @click="onGetCode">
          {{ countdown>0 ? `${countdown}s` : '获取验证码' }}
        </button>
      </view>
      <view class="divider"></view>
      
      <view class="field">
        <text class="label">输入新密码</text>
        <input class="input" v-model="password" type="password" placeholder="请输入新密码" />
      </view>
      <view class="divider"></view>
      
      <view class="field">
        <text class="label">确认密码</text>
        <input class="input" v-model="confirmPassword" type="password" placeholder="请再次输入新密码" />
      </view>
    </view>

    <button class="submit-btn" @click="onSubmit">
      确认提交
    </button>
  </view>
</template>

<script setup>
  import { ref, onMounted, onUnmounted } from 'vue';
  import sheep from '@/sheep';
  import AuthUtil from '@/sheep/api/member/auth';
  import UserApi from '@/sheep/api/member/user';

  const phone = ref('');
  const code = ref('');
  const password = ref('');
  const confirmPassword = ref('');
  const countdown = ref(0);
  let timer = null;

  // 获取用户信息
  const getUserInfo = async () => {
    try {
      const userInfo = await sheep.$store('user').getInfo();
      phone.value = userInfo.mobile || '';
    } catch (error) {
      console.error('获取用户信息失败:', error);
    }
  };

  onMounted(() => {
    getUserInfo();
  });

  const onGetCode = () => {
    if (countdown.value > 0) return;
    if (phone.value.length !== 11) {
      return uni.showToast({ title: '请输入正确手机号', icon: 'none' });
    }
    
    // scene=3 表示更改密码场景
    AuthUtil.sendSmsCode(phone.value, 3)
      .then(res => {
        if (res.code === 0) {
          uni.showToast({ title: '验证码已发送', icon: 'success' });
          countdown.value = 60;
          timer = setInterval(() => {
            countdown.value--;
            if (countdown.value <= 0) {
              clearInterval(timer);
              timer = null;
            }
          }, 1000);
        } else {
          uni.showToast({ title: res.msg || '发送失败', icon: 'none' });
        }
      })
      .catch(error => {
        console.error('发送验证码失败:', error);
        uni.showToast({ title: '发送失败，请重试', icon: 'none' });
      });
  };

  const onSubmit = () => {
    // 表单验证
    if (phone.value.length !== 11) {
      return uni.showToast({ title: '请输入正确手机号', icon: 'none' });
    }
    if (!code.value) {
      return uni.showToast({ title: '请输入验证码', icon: 'none' });
    }
    if (!password.value) {
      return uni.showToast({ title: '请输入新密码', icon: 'none' });
    }
    if (password.value !== confirmPassword.value) {
      return uni.showToast({ title: '两次密码输入不一致', icon: 'none' });
    }
    
    // 准备提交数据
    const updateData = {
      mobile: phone.value,
      code: code.value,
      password: password.value
    };
    
    // 调用更改密码接口
    UserApi.updateUserPassword(updateData)
      .then(res => {
        if (res.code === 0) {
          uni.showToast({ title: '密码修改成功', icon: 'success' });
          setTimeout(() => {
            uni.navigateBack();
          }, 1500);
        } else {
          uni.showToast({ title: res.msg || '修改失败', icon: 'none' });
        }
      })
      .catch(error => {
        console.error('修改密码失败:', error);
        uni.showToast({ title: '修改失败，请重试', icon: 'none' });
      });
  };

  // 返回上一页（与登录页一致逻辑）
  const goBack = () => {
    const pages = getCurrentPages();
    if (pages.length > 1) {
      uni.navigateBack();
    } else {
      uni.switchTab({ url: '/pages/index/index' });
    }
  };

  // 页面卸载时清理定时器
  onUnmounted(() => {
    if (timer) {
      clearInterval(timer);
      timer = null;
    }
  });
</script>

<style scoped>  
  .change-password-page {
    min-height: 100vh;
    background: #F6F6F6;
    padding: 0 0 40rpx 0;
  }
  .header {
    position: relative;
    height: 480rpx;
    width: 100%;
    margin-bottom: 0;
  }
.back-button {
  position: absolute;
  left: 20rpx;
  top: 60rpx;
  width: 80rpx;
  height: 80rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  backdrop-filter: blur(10rpx);
}
.back-icon {
  width: 100rpx;
  height: 100rpx;
}
  .header-image {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 1;
  }
  .hero-content {
    position: absolute;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding: 0rpx 50rpx;
    z-index: 20;
  }
  
  /* 左侧文本样式 */
  .hero-text {
    flex: 1;
    padding-right: 30rpx;
  }
  .welcome { 
    display: block; 
    font-size: 48rpx; 
    font-weight: 800; 
    color: #000000; 
    margin-bottom: 12rpx; 
    margin-top: -250rpx;
  }
  .slogan { 
    display: block; 
    font-size: 26rpx; 
    color: #666; 
  }
  .hero-image {
    width: 321rpx;
    height: 306rpx;
    margin-top: -300rpx;
    margin-right: 53rpx;
  }
  .hero-image image {
    width: 100%;
    height: 100%;
    border-radius: 24rpx;
  }
  .card { 
    background: #fff; 
    border-radius: 20rpx; 
    padding: 24rpx; 
    margin-top: -40rpx; 
    margin-bottom: 100rpx;
    margin-left: 17rpx; 
    margin-right: 17rpx;
    z-index: 30;
    position: relative;
  }
  .field {
    margin-top: 20rpx;
    margin-bottom: 8rpx; 
  }
  .row { 
    display: flex; 
    align-items: center; 
    gap: 16rpx; 
  }
  .col { 
    flex: 1; 
  }
  .label { 
    display: block; 
    font-size: 30rpx; 
    font-weight: 700; 
    color: #1f2937; 
    padding: 10rpx 20rpx;
  }
  .input { 
    width: 100%; 
    height: 80rpx; 
    border: 0; 
    background: transparent; 
    font-size: 28rpx; 
    color: #111; 
    padding: 0rpx 20rpx;
  }
  .input::placeholder {
    color: #c0c4cc; 
  }
  .phone-display {
    font-size: 28rpx;
    color: #666;
    padding: 0rpx 20rpx;
    line-height: 80rpx;
  }
  .divider { 
    height: 2rpx; 
    background: #f1f2f6; 
    margin: 8rpx 20rpx 20rpx 20rpx; 
  }
  .code-btn { 
    padding: 0 20rpx; 
    height: 72rpx; 
    line-height: 72rpx; 
    border-radius: 36rpx; 
    color: #fff; 
    font-size: 26rpx; 
    background: linear-gradient(90deg,#a78bfa,#60a5fa); 
    margin-right: 15rpx;
    border: none;
  }
  .code-btn[disabled] { 
    opacity: .5; 
  }

  .submit-btn { 
    width: 85%; 
    height: 96rpx; 
    line-height: 96rpx; 
    border-radius: 56rpx; 
    color: #fff; 
    font-size: 30rpx; 
    background: linear-gradient(90deg, #B276FF 0%, #74A7FF 100%); 
    border: none;
    outline: none;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto;
  }
</style>
